function [r, k, u, c] = NewtonMethod( fx, a, b, eps )
fx = sym(fx);
a = double(sym(a));
b = double(sym(b));
f = inline(fx);
f1 = inline(diff(fx));
f2 = inline(diff(fx, 2));

r = 0;
k = 0;
u = 0;
c = true;

max = Inf;

for step = a : eps * 10 : b
    if abs(f(step) * f2(step)) >= f1(step)^2
        c = false;
        max = 500;
    end;
end;

formula = 'xp - f(xp) / f1(xp)';
if (f(a) * f2(a) > 0)
    xp = a;
else 
    xp = b;
end;
xk = eval(formula);
i = 1;
while(abs(xk - xp) >= eps && i < max)
    xp = xk;
    xk = eval(formula);
    i = i + 1;
end;
if (i < max)
    c = true;
end;
r = xk;
k = i;
u = f(xk);

end

